<?php

/**
 * 自动生成 WxEventCupidUserController
 * 
 * Apps/Mp/Controller/WxEventCupidUserController.class.php
 * Zix @ 2015-08-12
 */

namespace Mp\Controller;

use Common\Service\WxEventCupidUserService;

class WxEventCupidUserController extends MpController {

  public function index($event_type = '', $event_id = 0) {
    $action = I('get.action', 'read');

    $event_obj_name = 'Common\Service\WxEvent' . $event_type . 'Service';
    $EventObj = $event_obj_name::instance();
    $event_data = $EventObj->get_by_id($event_id);

    if ($event_data['mer_id'] != $this->mer_id) {
      gameover('商户信息错误');
    }

    $data = $this->_init_data();
    $data['module_name'] = '参与用户';
    $data['module_url'] = U($this->base_path . 'index');

    $data['event_name'] = $EventObj->name . ' 活动';
    $data['event_url'] = U('Mp/WxEvent' . $event_type . '/index') . '/' . $event_data['id'];

    $WxEventCupidUser = WxEventCupidUserService::instance();
    $data['config'] = array(
      'action' => $action,
      'action_label' => $this->action_label[$action],
      'menu_id' => 'menu_Mp_MerWxSetting_index',
      'event_type' => $event_type,
      'event_id' => $event_id,
      'module_url' => U($this->base_path . 'index'),
      'read_url' => U($this->base_path . 'read'),
      'create_url' => U($this->base_path . 'create'),
      'update_url' => U($this->base_path . 'update'),
      'delete_url' => U($this->base_path . 'delete'),
      'export_user_total_excel_url' => U($this->base_path . 'export_user_total_excel'),
      'page' => 1,
      'page_size' => C('PAGE_SIZE'),
      'row' => $WxEventCupidUser->get_default_row()
    );

    $data['param'] = array(
      'award_status' => $WxEventCupidUser->award_status
    );

    $data['user_count'] = $WxEventCupidUser->count_user($event_id);

    $this->display_with_layout($data, $this->controller . ':index');
  }

  function read() {
    $post = I('post.');

    $config = array(
      'event_type' => $post['event_type'],
      'event_id' => $post['event_id'],
      'page' => $post['page'],
      'page_size' => $post['page_size'],
      'sort' => empty($post['sort']) ? 'id' : $post['sort'],
      'order' => empty($post['order']) ? 'DESC' : $post['order'],
    );

    $WxEventCupidUser = WxEventCupidUserService::instance();
    $data['rows'] = $WxEventCupidUser->get_by_cond($config);

    $config['count'] = TRUE;
    $data['total'] = $WxEventCupidUser->get_by_cond($config);

    $this->ajaxReturn($data);
  }

  function create() {
    $post = I('post.');
    if (empty($post)) {
      $this->ajaxReturn(ajax_arr('数据错误,请稍后再试'));
    }

    $WxEventCupidUser = WxEventCupidUserService::instance();
    $ret = $WxEventCupidUser->create($post);
    $this->ajaxReturn($ret);
  }

  function update($id = 0) {
    $post = I('post.');
    if (empty($post)) {
      $this->ajaxReturn(ajax_arr('数据错误,请稍后再试'));
    }

    $WxEventCupidUser = WxEventCupidUserService::instance();
    $ret = $WxEventCupidUser->update($id, $post);
    $this->ajaxReturn($ret);
  }

  function delete() {
    $ids = I('post.ids');
    $WxEventCupidUser = WxEventCupidUserService::instance();
    $ret = $WxEventCupidUser->delete($ids);
    $this->ajaxReturn($ret);
  }

  function export_user_total_excel() {
    $WxEventCupidUser = WxEventCupidUserService::instance();

    $data = $WxEventCupidUser->get_user_award_total();

    $title = array(
      array('uid', 'ID'),
      array('nickname', '昵称'),
      array('truename', '真名'),
      array('phone', '电话'),
      array('award_amount', '中奖次数'),
      array('untake_amount', '未领奖次数'),
      array('taked_amount', '已领奖次数'),
      array('award_time' , '中奖时间')
    );

    $filename = '月老帮帮来牵线中奖汇总表_'.date('Y_m_d');

    $excel = \Common\Service\ExcelService::instance();
    $excel->export($filename, $title, $data, TRUE, FALSE);
  }

}
